Welcome to Django!

2.8 路由分发

将path 路由迁移到hello模块

hello 模块中导入

from django.urls import path

from . import views

urlpatterns = [

path( 'hello' , views.hello),

]


urlpatterns = [

re_path(r "^ariticles/([0-9]{4})$" , views.year_archive) ,    #无名分组

re_path(r "^ariticles/(?P <year>[0-9]{4})/(?P <month>[0-9]{2})$" , views.month_archive),

# 有名分组,P必须是大写的,不然会报错

]


在总路由中导入include()函数导入上面的分发路由

from django.urls import path,re_path,include     #导入include函数

from django.contrib import admin


urlpatterns = [

path( 'admin/' , admin.site.urls),

path( 'hello/' , include("hello.urls")),      #导入include函数,传入文件

re_path( 'ariticles/' , include( "ariticles.urls")),        #总路是ariticles/+后面分发的一段

]


打开的地址如下: 127.0.0.1:8000/ariticles/ariticles/2023/11